//
//  SlotsVC.m
//  Turntable
//
//  Created by yaqiang bi on 2017/3/10.
//  Copyright © 2017年 yaqiang bi. All rights reserved.
//

#import "SlotsVC.h"
#define AnimationDuration 0.5 
#define screenHeight [[UIScreen mainScreen] bounds].size.height                 //屏幕宽度
#define screenWidth [[UIScreen mainScreen] bounds].size.width                   //屏幕高度
@interface SlotsVC ()<CAAnimationDelegate>
{
    BOOL _isRequest;                //是否正在抽奖
    BOOL _isResult;                 //是否有抽奖结果
    NSInteger _index;               //动画位置
    NSInteger slowCount;            //开始减速要走的格数
    CGFloat width;
    CGFloat height;
}
@property (nonatomic,strong) UIImageView *turnTable;        //转盘
@property (nonatomic,strong) CADisplayLink *link;           //无限动画
@property (nonatomic,strong) CADisplayLink *slowLink;       //减速动画
@property (nonatomic,strong) UIView* hightView;             //高亮视图
@property (nonatomic,strong) NSArray *slotArr;
@end

@implementation SlotsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
        [self initViews];
    self.slotArr = @[[NSValue valueWithCGPoint:CGPointMake(0, 0)],[NSValue valueWithCGPoint:CGPointMake(1, 0)],[NSValue valueWithCGPoint:CGPointMake(2, 0)],[NSValue valueWithCGPoint:CGPointMake(0, 1)],[NSValue valueWithCGPoint:CGPointMake(2, 1)],[NSValue valueWithCGPoint:CGPointMake(0, 2)],[NSValue valueWithCGPoint:CGPointMake(1, 2)],[NSValue valueWithCGPoint:CGPointMake(2, 2)]];
    
}
#pragma -mark 界面搭建
-(void)initViews{
    UIImageView *back = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    back.image = [UIImage imageNamed:@"slotBack"];
    back.userInteractionEnabled = YES;
    [self.view addSubview:back];
    
    UIImage *turanTableImage = [UIImage imageNamed:@"slot"];
    width = screenWidth*0.8/3.0;
    height = width*turanTableImage.size.height/turanTableImage.size.width;
    self.turnTable = [[UIImageView alloc]initWithFrame:CGRectMake((screenWidth-3*width)/2.0, (screenHeight-3*height)/2.0, width*3, height*3)];
    self.turnTable.image = turanTableImage;
    self.turnTable.userInteractionEnabled = YES;
    [back addSubview:_turnTable];
    
    UIButton *drawBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    drawBtn.frame = CGRectMake(width, height, width, height);
    [drawBtn setBackgroundImage:[UIImage imageNamed:@"slotButton"] forState:UIControlStateNormal];
    [drawBtn addTarget:self action:@selector(draw:) forControlEvents:UIControlEventTouchUpInside];
    [_turnTable addSubview:drawBtn];
    

    self.hightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
    self.hightView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    self.hightView.hidden = YES;
    [_turnTable addSubview:_hightView];
}
#pragma -mark Action
//开始抽奖
-(void)draw:(UIButton*)sender{
    if (_isRequest) {
        _isRequest = NO;
        _isResult = YES;
        [self.link invalidate];
        self.link = nil;
        
        NSInteger count = 8;
        int random = arc4random()%count+1;
        slowCount = 2*count-_index+random+1;              //剩下的路
        [self resultAnimation:slowCount-1];
    }else{
        self.hightView.hidden = NO;
        _isRequest = YES;
        _isResult = NO;
        self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(firmlyAnimation)];
        self.link.preferredFramesPerSecond = 8/AnimationDuration;
        [_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
        _index = 0;
    }
    
}
//抽奖结果未返回的动画
-(void)firmlyAnimation{
    NSInteger count = 8;
    CGPoint point = [self.slotArr[_index++] CGPointValue];
    self.hightView.frame = CGRectMake(point.x*width, point.y*height, width, height);
    if (_index == count) {
        _index = 0;
    }
}
//抽奖结果返回的动画
-(void)resultAnimation:(NSInteger)Count{
    if (Count) {
        NSInteger count = 8;

        CGPoint point = [self.slotArr[_index++] CGPointValue];
        if (_index == count) {
            _index = 0;
        }
        CGFloat padding = AnimationDuration/slowCount;
        Count -= 1;
        CGFloat time = (slowCount-Count)*padding;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.hightView.frame = CGRectMake(point.x*width, point.y*height, width, height);
            [self resultAnimation:Count];
        });
        
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
